package com.wing.order.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.cabinet.model.response.AddAlarmVO;
import com.wing.common.exception.BusinessException;
import com.wing.order.dao.InventoryAlarmDao;
import com.wing.order.model.entity.DistributionDemandPlan;
import com.wing.order.model.entity.DistributionDemandPlanDetail;
import com.wing.order.model.entity.InventoryAlarm;
import com.wing.order.model.request.InventoryAlarmForm;
import com.wing.order.model.response.AlarmProductVo;
import com.wing.order.model.response.InventoryAlarmDetailVO;
import com.wing.order.model.response.InventoryAlarmVO;
import com.wing.order.model.response.RecycleVo;
import com.wing.product.model.entity.ProductAttribute;
import com.wing.product.model.entity.ProductAttributeValue;
import com.wing.product.service.ProductAttributeService;
import com.wing.product.service.ProductAttributeValueService;
import com.wing.web.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;

/**
 * 库存告警-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class InventoryAlarmServiceImpl extends ServiceImpl<InventoryAlarmDao, InventoryAlarm> implements InventoryAlarmService {

    @Autowired
    private ProductAttributeService attributeService;
    @Autowired
    private ProductAttributeValueService productAttributeValueService;

    @Autowired
    private DistributionDemandPlanService demandPlanService;

    @Autowired
    private  DistributionDemandService distributionDemandService;

    @Autowired
    private DistributionDemandPlanService distributionDemandPlanService;

    @Autowired
    private DistributionDemandPlanDetailService distributionDemandPlanDetailService;

    @Autowired
    private DynamicService dynamicService;

    @Autowired
    private InventoryAlarmDetailService inventoryAlarmDetailService;

    @Override
    public int add(InventoryAlarmForm inventoryAlarmForm) {
        InventoryAlarm inventoryAlarm = new InventoryAlarm();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(InventoryAlarm.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(inventoryAlarmForm, inventoryAlarm, copyOptions);

        return baseMapper.insert(inventoryAlarm);
    }

    @Override
    public List<InventoryAlarmVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<InventoryAlarmVO> pageList4app(Page page, Map<String, Object> params) {
        List<InventoryAlarmVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public InventoryAlarmVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    public Page<InventoryAlarmVO> tellWarn(Page<InventoryAlarmVO> page, Map<String, Object> params) {
        List<InventoryAlarmVO> list = baseMapper.tellWarn(page, params);
        InventoryAlarm inventoryAlarm = null;
        InventoryAlarmVO inventoryAlarmVO = null;
        for (int i = 0; i <list.size() ; i++) {
            inventoryAlarmVO = list.get(i);
            inventoryAlarmVO.setCreateTime(LocalDateTime.now());
        }
        List<InventoryAlarmVO>list1 = new ArrayList<>();
        list1.add(inventoryAlarmVO);
        page.setRecords(list1);
        return page;
    }

    @Override
    public List<RecycleVo> recycleInfo() {

        return baseMapper.recycleInfo();
    }

    @Override
    public Page<InventoryAlarmVO> alarmProduct(Page<InventoryAlarmVO> page, Map<String, Object> params,String cabCode) {
        List<InventoryAlarmVO> voList = baseMapper.alarmProduct(page, params, cabCode);
        page.setRecords(voList);
        return page;
    }

    @Override
    public boolean applyRepair(String skuCode, String cabCode) {
        InventoryAlarmVO inventoryAlarmVO = baseMapper.applyRepair(skuCode, cabCode);
        DistributionDemandPlan distributionDemandPlan = new DistributionDemandPlan();
        inventoryAlarmVO.setStatus(InventoryAlarm.Status.replenishment);

        BeanUtil.copyProperties(inventoryAlarmVO,distributionDemandPlan);
        distributionDemandPlan.setStatus(DistributionDemandPlan.Status.replay);
        //需求编号
        distributionDemandPlan.setDisbCode(UUID.randomUUID().toString());
        distributionDemandPlan.setId(null);
        distributionDemandPlan.setCreatorName(DateTime.now().toString());
        if (!demandPlanService.save(distributionDemandPlan)) {
            return false;
        }
        InventoryAlarm inventoryAlarm = new InventoryAlarm();
        BeanUtil.copyProperties(inventoryAlarmVO,inventoryAlarm);
        if (!this.updateById(inventoryAlarm)){
            return false;
        }
        return true;
    }

    @Override
    public AlarmProductVo alarmProductDetails(String skuCode) {
        //告警产品信息
        AlarmProductVo alarmProductVo = baseMapper.AlarmProductDetails(skuCode);
        //获取属性
        ProductAttribute attribute = attributeService.getById(alarmProductVo.getProductTypeId());
        //属性值
        if (attribute!=null&&attribute.getProductType()!=null){

            ProductAttributeValue value = productAttributeValueService.getById(attribute.getProductType());
            List<ProductAttribute>list = new ArrayList<>();
            List<ProductAttributeValue>valueslist = new ArrayList<>();
            valueslist.add(value);
            ProductAttribute productAttribute = new ProductAttribute();
            BeanUtil.copyProperties(alarmProductVo,productAttribute);
            productAttribute.setProductAttributeValue(valueslist);
            list.add(productAttribute);
            alarmProductVo.setProductAttribute(list);
        }



        return alarmProductVo;
    }

    @Override
    public Page<AlarmProductVo> proInventory(Page<AlarmProductVo> page, Map<String, Object> params, String cabCode) {
        List<AlarmProductVo> voList = baseMapper.proInventory(page, params, cabCode);
        page.setRecords(voList);
        return page;
    }

    @Override
    public Page<AlarmProductVo> spaceInventory(Page<AlarmProductVo> page, Map<String, Object> params, String cabCode) {
        List<AlarmProductVo> voList = baseMapper.spaceInventory(page, params, cabCode);
        page.setRecords(voList);
        return page;
    }

    @Override
    public AlarmProductVo repairRecordPro(String skuCode) {
        return baseMapper.repairRecordPro(skuCode);
    }

    @Override
    public List<AlarmProductVo> productDetails(String skuCode, String cabCode) {
        return baseMapper.productDetails(skuCode,cabCode);
    }

    @Override
    public List<AlarmProductVo> productDetails2(String skuCode, String cabCode) {
        return baseMapper.productDetails2(skuCode,cabCode);
    }

    @Override
    @Transactional
    public void confirm(InventoryAlarmForm inventoryAlarmForm) {
        InventoryAlarmVO inventoryAlarmVO=baseMapper.get4appById(inventoryAlarmForm.getId());
        if (inventoryAlarmForm.getPlanReplenishment()==null){
            inventoryAlarmForm.setPlanReplenishment(inventoryAlarmVO.getProposalStock());
        }
        if (inventoryAlarmVO==null){
            throw new BusinessException("未找到告警业务数据");
        }
        InventoryAlarm inventoryAlarm4Update=new InventoryAlarm();
        inventoryAlarm4Update.setId(inventoryAlarmForm.getId());
        inventoryAlarm4Update.setStatus(InventoryAlarm.Status.confirmed);
        inventoryAlarm4Update.setPlanReplenishment(inventoryAlarmForm.getPlanReplenishment());
        baseMapper.updateById(inventoryAlarm4Update);
        LocalDateTime now=LocalDateTime.now();
        DistributionDemandPlan distributionDemandPlan=new DistributionDemandPlan();
        BeanUtils.copyProperties(inventoryAlarmVO,distributionDemandPlan);
        distributionDemandPlan.setId(null);
        distributionDemandPlan.setPlanReplenishment(inventoryAlarmForm.getPlanReplenishment());
        String code = dynamicService.getDayNextCode("ord_distribution_demand_plan");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        String day = simpleDateFormat.format(new Date());
        distributionDemandPlan.setDay(Integer.valueOf(day));
        distributionDemandPlan.setDisbCode(day + "-" + code);
        distributionDemandPlan.setNo(Integer.valueOf(code));
//        distributionDemandPlan.setEnterpriseId(Long.valueOf(BaseDataInit.ENTERPRISE_DATA.getId()));
        distributionDemandPlan.setCreator(JwtUtils.getLoginUserId());
        distributionDemandPlan.setCreatorName(JwtUtils.getLoginUsername());
        distributionDemandPlan.setStatus(DistributionDemandPlan.Status.replay);
        distributionDemandPlan.setAlarmId(inventoryAlarmForm.getId());
        distributionDemandPlan.setCreateTime(now);
        distributionDemandPlanService.save(distributionDemandPlan);

        Map paramMap=new HashMap();
        paramMap.put("alarmId",inventoryAlarmForm.getId());
        List<InventoryAlarmDetailVO> inventoryAlarmDetailVOS=inventoryAlarmDetailService.list4app(paramMap);
        for(InventoryAlarmDetailVO inventoryAlarmDetailVO:inventoryAlarmDetailVOS){
            DistributionDemandPlanDetail distributionDemandPlanDetail=new DistributionDemandPlanDetail();
            BeanUtils.copyProperties(inventoryAlarmDetailVO,distributionDemandPlanDetail);
            distributionDemandPlanDetail.setPlanId(distributionDemandPlan.getId());
            distributionDemandPlanDetail.setId(null);
            distributionDemandPlanDetail.setCreateTime(now);
            distributionDemandPlanDetailService.save(distributionDemandPlanDetail);
        }
    }

    @Override
    public List<AddAlarmVO> listAlarm() {
        return baseMapper.listAlarm();
    }


}
